// cloudfunctions/checkProductWantStatus/index.js
const cloud = require('wx-server-sdk');
cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV
});
const db = cloud.database();

exports.main = async (event, context) => {
  try {
    const { productId, userId } = event;
    
    // 验证参数
    if (!productId || !userId) {
      return { success: false, message: '缺少必要参数' };
    }
    
    // 查询用户是否已经想要该产品
    const wantResult = await db.collection('user_wants')
      .where({
        userId,
        productId,
        status: 'active'
      })
      .get();
    console.log(wantResult);
    return {
      success: true,
      isWanted: wantResult.data.length > 0,
      message: '查询成功'
    };
  } catch (error) {
    console.error('查询想要状态失败', error);
    return {
      success: false,
      message: error.message || '服务器错误',
      error: error.message
    };
  }
};